<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of CDBProvider
 *
 * @author wanghao
 */
class CDBProvider extends CDBProvider_SAE {

    protected $table_name = "";

    /** 保存
     *
     * @param <type> $arr
     * @return <type>
     */
    function save($arr) {
        if (isset($arr["id"]) && intval($arr["id"]) > 0) {
            $sql = CSql::update($arr, $this->table_name, "where id=" . $arr["id"]);
            $this->runSql($sql);
            return $this->affected_rows();
        } else {
            unset($arr["id"]);
            $sql = CSql::insert($arr, $this->table_name);
            $this->runSql($sql);
            return $this->insert_id();
        }
    }

    /** 删除
     *
     * @param <type> $id
     * @return <type>
     */
    function del($id) {
        $sql = "delete from " . $this->table_name . " where id=" . intval($id);
        $this->runSql($sql);
        return $this->affected_rows();
    }

    /** 列表
     *
     * @param <type> $page
     * @param <type> $page_size
     * @param <type> $where
     */
    function lists($page=1, $page_size=0, $where="") {
        $page = intval($page);
        $page_size = intval($page_size);
        $start = ($page - 1) * $page_size;
        if ($page_size) {
            $limit = " limit $start,$page_size";
        }
        $sql = "select * from " . $this->table_name . " " . $where . $limit;
        return $this->runSql($sql);
    }

    /** 记录条数
     *
     * @param <type> $where
     * @return <type>
     */
    function num($where="") {
        $sql = "select count(1) as num from " . $this->table_name . " " . $where;
        $ret = $this->runSql($sql);
        return intval($ret[0]["num"]);
    }

    /** 通过id获取
     *
     * @param <type> $id
     * @return <type>
     */
    function oneByID($id) {
        $sql = "select * from " . $this->table_name . " where id=" . intval($id);
        $ret = $this->runSql($sql);
        if ($this->isRet($ret)) {
            return $ret[0];
        }
        return null;
    }

}

?>
